/**
 * Copyright (c) 2006-2016 Huize Ltd. All Rights Reserved. 
 *  
 * This code is the confidential and proprietary information of   
 * Hzins. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with Huize,http://www.huize.com.
 *  
 */
package com.agan.concurrent;

import java.util.concurrent.Exchanger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author hz1412988
 * @date 2018年2月5日 下午3:28:36
 * @version
 */
public class ExchangerTest {

    public static void main(String[] args) {
	ExecutorService threadPool = Executors.newCachedThreadPool();
	final Exchanger<String> exchanger = new Exchanger<String>();

	threadPool.execute(new Runnable() {

	    @Override
	    public void run() {
		String data = "人质B";
		try {
		    String response = exchanger.exchange(data);
		    System.out.println("绑架者用B换回" + response);
		} catch (InterruptedException e) {
		    e.printStackTrace();
		}
	    }
	});

	// 家属c
	threadPool.execute(new Runnable() {

	    @Override
	    public void run() {
		String data = "1000万";
		try {
		    String response = exchanger.exchange(data);
		    System.out.println("C用1000万换回" + response);
		} catch (InterruptedException e) {
		    e.printStackTrace();
		}
	    }
	});

	threadPool.shutdown();
    }
}
